﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;

using Ahi.Common.Base;
using MongoDB.Driver.Builders;
using Ahi.Config;

namespace Ahi.Controller
{
    public class MongoAdaptor
    {
        private MongoServer server;
        private MongoDatabase db;
        private MongoCollection collection;
        private string collectionName;

        public MongoAdaptor(string collectionName)
        {
            this.collectionName = collectionName;

            server = MongoServer.Create();
            db = server.GetDatabase(AppConfig.Current.DatabaseAdi);
            collection = db.GetCollection(this.collectionName);
        }

        public void Update<T>(T entity)
        {
            collection.Save<T>(entity);
        }

        public List<T> GetCollection<T>()
        {
            var find = collection.FindAllAs(typeof(T));
            return find.Cast<T>().ToList();
        }

        public T GetItem<T>(ObjectId objectId)
        {
            return collection.FindOneAs<T>(Query.EQ("_id", objectId));
        }

        public void Update<T>(List<T> liste)
        {
            foreach (var item in liste)
            {
                Update<T>(item);
            }
        }


    }
}
